package com.srwl.mytx.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.srwl.mytx.Constant;
import com.srwl.mytx.R;
import com.srwl.mytx.activity.ChatActivity;
import com.srwl.mytx.utils.ClickUtil;
import com.srwl.mytx.utils.GlideLoadUtils;
import com.srwl.mytx.widget.UserDetailsDialog;
import com.srwl.mytx.activity.UserProfileActivity;
import com.srwl.mytx.domain.User;

import java.util.List;

public class AvatarAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<User> userlist;
    private Context context;

    private final static int ANCHOR_AVATAR_ITEM = 0;//主播头像
    private final static int AUDIENCE_AVATAR_ITEM = 1;//观众头像


    public AvatarAdapter(Context context, List<User> userlist) {
        this.userlist = userlist;
        this.context = context;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return new AudienceAvatarViewHolder(LayoutInflater.from(context).inflate(R.layout.avatar_list_item, parent, false));

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        if (holder instanceof AudienceAvatarViewHolder) {
            User user = userlist.get(position);
            AudienceAvatarViewHolder viewHolder = (AudienceAvatarViewHolder) holder;
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (ClickUtil.isFastClick()) {
                        return;
                    }
                    if (user.getGender() == null) {
                        user.setGender(0);
                    }
                    Intent intent = new Intent(context, UserProfileActivity.class);
                    intent.putExtra(Constant.EXTRA_TARGET_USER_INFO, user);
                    showUserDetailsDialog(user);
                    //context.startActivity(intent);//跳转到用户详情页面
                }
            });
//            RequestOptions options = new RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL).error(R.drawable.tx_default_avatar_1);
//
//            Glide.with(context)
//                    .load(user.getAvatar())
//                    .apply(options)
//                    .into(viewHolder.Avatar);

            GlideLoadUtils.glideLoad(context,user.getAvatar(),viewHolder.Avatar);
        }
//        else {
//            AnchorAvatarViewHolder viewHolder = (AnchorAvatarViewHolder) holder;
//            holder.itemView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    User user = userlist.get(position);
//                    if (user.getGender() == null) {
//                        user.setGender(0);
//                    }
//                    Intent intent = new Intent(context, UserProfileActivity.class);
//                    intent.putExtra(Constant.EXTRA_TARGET_USER_INFO, user);
//                    context.startActivity(intent);//跳转到用户详情页面
//
//                    //showUserDetailsDialog(userlist.get(position));
//                }
//            });
//            RequestOptions options = new RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL).error(R.drawable.tx_default_avatar);
//            viewHolder.tv_anchorname.setText(userlist.get(position).getUsername());
//            Glide.with(context)
//                    .load(userlist.get(position).getAvatar())
//                    .apply(options)
//                    .into(viewHolder.iv_anchorAvatar);
//
//        }
    }

    private void showUserDetailsDialog(User user) {
//        FragmentActivity fragment = (FragmentActivity) context;
//        FragmentManager fragmentManager = fragment.getSupportFragmentManager();
//        final UserDetailsDialog dialog = UserDetailsDialog.newInstance(user);
//        dialog.setUserDetailsDialogListener(
//                new UserDetailsDialog.UserDetailsDialogListener() {
//                    @Override
//                    public void onMessageClick(User user) {
//                        dialog.dismiss();
//                        //  Bundle bd = new Bundle();
//                        // bd.putCharSequence("userId", user.getuId());
//                        // bd.putParcelable(Constant.EXTRA_TARGET_USER_INFO, user);
////                        ChatFragment chatFragment = new ChatFragment();
////                        chatFragment.setArguments(bd);
////
////                        chatFragment.show(fragmentManager, "ChatFragment");
//
//                        context.startActivity(new Intent(context, ChatActivity.class).putExtra(Constant.EXTRA_TARGET_USER_INFO, user));
//                    }
//
//                    @Override
//                    public void onMentionClick(String username) {
//                        dialog.dismiss();
////                        messageView.getInputView().setText("@" + username + " ");
////                        showInputView();
//                    }
//                });
//        dialog.show(fragmentManager, "UserDetailsDialog");

        UserDetailsDialog detailsDialog_1 = new UserDetailsDialog(context, user);
        detailsDialog_1.setUserDetailsDialogListener(new UserDetailsDialog.UserDetailsDialogListener() {
            @Override
            public void onMentionClick(String username) {

            }

            @Override
            public void onMessageClick(User user) {
                context.startActivity(new Intent(context, ChatActivity.class).putExtra(Constant.EXTRA_TARGET_USER_INFO, user));
            }
        });
        detailsDialog_1.show();
    }


    @Override
    public int getItemCount() {
        return userlist == null ? 0 : userlist.size();
    }


    class AudienceAvatarViewHolder extends RecyclerView.ViewHolder {
        ImageView Avatar;

        public AudienceAvatarViewHolder(View itemView) {
            super(itemView);
            Avatar = itemView.findViewById(R.id.avatar);

        }
    }

    class AnchorAvatarViewHolder extends RecyclerView.ViewHolder {
        ImageView iv_anchorAvatar;
        TextView tv_anchorname;

        public AnchorAvatarViewHolder(View itemView) {
            super(itemView);
            tv_anchorname = itemView.findViewById(R.id.tv_anchor_nick);
            iv_anchorAvatar = itemView.findViewById(R.id.img_icon);
        }
    }

}


